package com.lsl.endworkvol.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lsl.endworkvol.entity.Admin;
import com.lsl.endworkvol.entity.Leader;
import com.lsl.endworkvol.entity.Volunteer;
import com.lsl.endworkvol.service.AdminService;
import com.lsl.endworkvol.service.LeaderService;
import com.lsl.endworkvol.service.VolunteerService;

@Controller
public class UserInfoController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private LeaderService leaderService;

    @Autowired
    private VolunteerService volunteerService;

    @RequestMapping(path = "/admin/myinfo_page", method = RequestMethod.GET)
    public ModelAndView toMyInfoPageOfAdmin(ModelAndView modelAndView, HttpServletRequest request) {
        modelAndView.setViewName("admin/myinfo");
        Admin userNow = (Admin)adminService.getUser(
            Integer.parseInt(request.getSession().getAttribute("userid").toString())
        );
        modelAndView.addObject("username", userNow.getUsername());
        return modelAndView;
    }

    @RequestMapping(path = "/leader/myinfo_page", method = RequestMethod.GET)
    public ModelAndView toMyInfoPageOfLeader(ModelAndView modelAndView, HttpServletRequest request) {
        modelAndView.setViewName("leader/myinfo");
        Leader userNow = (Leader)leaderService.getUser(
            Integer.parseInt(request.getSession().getAttribute("userid").toString())
        );
        modelAndView.addObject("username", userNow.getUsername());
        modelAndView.addObject("phone", userNow.getPhone());
        modelAndView.addObject("email", userNow.getEmail());
        return modelAndView;
    }

    @RequestMapping(path = "/volunteer/myinfo_page", method = RequestMethod.GET)
    public ModelAndView toMyInfoPageOfVolunteer(ModelAndView modelAndView, HttpServletRequest request) {
        modelAndView.setViewName("volunteer/myinfo");
        Volunteer userNow = (Volunteer)volunteerService.getUser(
            Integer.parseInt(request.getSession().getAttribute("userid").toString())
        );
        modelAndView.addObject("vol_id", userNow.getVolId());
        modelAndView.addObject("username", userNow.getName());
        modelAndView.addObject("sex", userNow.getSex());
        modelAndView.addObject("phone", userNow.getPhone());
        modelAndView.addObject("email", userNow.getEmail());
        modelAndView.addObject("majorclass", userNow.getMajorclass());
        modelAndView.addObject("caption", userNow.getCaption());
        return modelAndView;
    }

    @RequestMapping(path = "/admin/myinfo_edit", method = RequestMethod.POST)
    public String adminInfoEdit(@RequestParam Map<String,String> params, HttpServletRequest request) {
        if (!params.get("oldpwd").equals(
            ((Admin)adminService.getUser(
                Integer.parseInt(request.getSession().getAttribute("userid").toString())
            )).getPassword()
        )) {
            return "redirect:/admin/warning/info_edit_failed_pwd_wrong";
        }
        boolean flag = adminService.editUser(() -> {
            Admin admin = new Admin();
            admin.setId(Integer.parseInt(request.getSession().getAttribute("userid").toString()));
            admin.setUsername(params.get("username"));
            admin.setPassword(params.get("newpwd") == null ? "" : params.get("newpwd"));
            return admin;
        });
        if (flag) {
            return "redirect:/admin/message/info_edit_success";
        } else {
            return "redirect:/admin/warning/info_edit_failed";
        }
    }

    @RequestMapping(path = "/leader/myinfo_edit", method = RequestMethod.POST)
    public String leaderInfoEdit(@RequestParam Map<String,String> params, HttpServletRequest request) {
        if (!params.get("oldpwd").equals(
            ((Leader)leaderService.getUser(
                Integer.parseInt(request.getSession().getAttribute("userid").toString())
            )).getPassword()
            )) {
                return "redirect:/leader/warning/info_edit_failed_pwd_wrong";
        }
        boolean flag = leaderService.editUser(() -> {
            Leader leader = new Leader();
            leader.setId(Integer.parseInt(request.getSession().getAttribute("userid").toString()));
            leader.setUsername(params.get("username"));
            leader.setPassword(params.get("newpwd") == null ? "" : params.get("newpwd"));
            leader.setPhone(params.getOrDefault("phone", ""));
            leader.setEmail(params.getOrDefault("email", ""));
            return leader;
        });
        if (flag) {
            return "redirect:/leader/message/info_edit_success";
        } else {
            return "redirect:/leader/warning/info_edit_failed";
        }
    }

    @RequestMapping(path = "/volunteer/myinfo_edit", method = RequestMethod.POST)
    public String volunteerInfoEdit(@RequestParam Map<String,String> params, HttpServletRequest request) {
        if (!params.get("oldpwd").equals(
            ((Volunteer)volunteerService.getUser(
                Integer.parseInt(request.getSession().getAttribute("userid").toString())
            )).getPassword()
        )) {
            return "redirect:/volunteer/warning/info_edit_failed_pwd_wrong";
        }
        boolean flag = volunteerService.editUser(() -> {
            Volunteer volunteer = new Volunteer();
            volunteer.setId(Integer.parseInt(request.getSession().getAttribute("userid").toString()));
            volunteer.setName(params.get("username"));
            volunteer.setPassword(params.get("newpwd") == null ? "" : params.get("newpwd"));
            volunteer.setSex(params.getOrDefault("sex", ""));
            volunteer.setPhone(params.getOrDefault("phone", ""));
            volunteer.setEmail(params.getOrDefault("email", ""));
            volunteer.setMajorclass(params.getOrDefault("majorclass", ""));
            volunteer.setCaption(params.getOrDefault("caption", ""));
            return volunteer;
        });
        if (flag) {
            return "redirect:/volunteer/message/info_edit_success";
        } else {
            return "redirect:/volunteer/warning/info_edit_failed";
        }
    }

}

